<?php
if(!defined('IN')) exit('Access Denied');
/*
	function list
	bool fheader()	//网站头部
	bool ffooter()	//网站尾部
*/

//网站头部
function fheader($css=''){
	global $smarty;
	//empty($title)?$title='宠物交友':$title.='-宠物交友';
	//$keywords.='宠物交友 宠物大全 宠物分类 宠物 宠物中国 大全 宠物龙猫 哈士奇 萨摩耶 金毛 拉布拉多 比熊 博美犬  藏獒 仓鼠 ';
	//$smarty->assign('h_title',$title);
	//$smarty->assign('h_keywords',$keywords);
	//$smarty->assign('h_description',$description);
	//$smarty->assign('h_onload',$onload);
	$smarty->assign('css',$css);
	$smarty->assign('v',time());
	$smarty->display("header.dwt");
}
//网站尾部
function ffooter(){
	global $smarty;
	$smarty->display("footer.dwt");
}

//字符串截取
function csubstr($str,$len,$tell=1){
	$str = mb_convert_encoding($str,'GBK','utf-8');
	$maxlen=0;
	for($i=0; $i<$len; $i++){//i是第几个字
		if(strlen(mb_substr($str,$i,1,'GBK'))>1) {
			$maxlen+=2;
		}else {
			$maxlen++;
		}
		if($maxlen>$len) break;
	}
	$laststr=mb_substr($str,0,$i,'GBK');
	if($laststr<>$str && $tell==1) $laststr.="...";
	return mb_convert_encoding($laststr,'utf-8','GBK');
}

//创建目录,注意目录后面附带"/"
function smdir($dir,$mark=0777){
	if(!is_dir($dir)){
		$c = explode( "/", $dir);
		array_pop($c);
		$mdir='';
		foreach( $c as $val ) {
			$mdir .= $val."/";
			if( $val == ".." || $val == "." ) continue;
			if(!file_exists( $mdir ) ) {
				if(!@mkdir( $mdir,$mark)){ die("创建目录 [".$mdir."]失败.");}
			}
		}
		@chmod(implode("/",$c), $mark);
	}
}
//拦截非法字符
function contentReplace($content){
	if(is_array($content)){
		foreach($content as $key => $val){
			$content[$key] = contentReplace($val);
		}
	}else{
		$content = saddslashes($content);
		$content = preg_replace(array("'<script[^>]*?>.*?</script>'si","'<head[^>]*?>.*?</head>'si","'<meta[^>]*?>.*?</meta>'si","'<title[^>]*?>.*?</title>'si","'<link[^>]*?>'si"),array("","","","",""),$content);
	}
	Return $content;
}
function saddslashes($string) {
	if (!get_magic_quotes_gpc()) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = saddslashes($val);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}
//cookie储存函数
function cookie($name,$value='',$time=0){
	global $Global;
	if($time==0) $time = -1000;
	setcookie($name, $value, $Global['F_time']+$time, '/', '.'.$Global['F_host']);
	//bug with localhost
	if($Global['F_host'] == 'localhost')setcookie($name, $value, $Global['F_time']+$time);
}
//得到cookie值
function getCookie($name){
	if(!empty($_COOKIE[$name])){
		Return $_COOKIE[$name];
	}else{
		Return false;
	}
}
//分析cookie值
function validate($z){
	global $Global;
	if(!empty($_COOKIE[$z])){
		$z = $_COOKIE[$z];
	}
	if(empty($z)) return false;
	$data = explode(':',$z);
	if(count($data) != 3) return false;
	if(md5($data[0].$data[1].$Global['cookie_hash']) == $data[2]) return $data;
	return false;
}
//未登录跳转
function islogin(){
	$data = empty($_COOKIE['chinapet'])? array() : validate('chinapet');
	if(empty($data)){
		header('Location: login.html');//如果没有登陆，跳转到提醒页面.
		exit;
	}else{
		Return $data;
	}
}
//计算天数
function datenum($time){
	global $Global;
	$datetime = $time-$Global['F_tdtime'];
	if($datetime>0){
		$num = ceil($datetime/86400);
		Return $num;
	}else{
		Return 0;
	}
}
//格式化时间
function rdate($timestamp = false, $format = ''){
	global $Global;
	if(false === $timestamp) $timestamp = $Global['F_time'];
	elseif($timestamp<1) Return '-';
	if(empty($format)) $format = 'Y-m-d H:i:s';
	return gmdate($format, $timestamp + $Global['time_zone'] * 3600 + $Global['time_delay']);
}
//今天零点整的时间值
function rtodaytime($timestamp = false){
	global $Global;
	$times = explode('-', rdate($timestamp, 'Y-m-d'));
	return gmmktime(0, 0, 0, $times[1], $times[2], $times[0]) - $Global['time_zone'] * 3600 - $Global['time_delay'];
}
function tagFormat($tag){	//标签格式化
	//当标签以 空格 或者 中英文逗号 分隔时，统一转化为 英文逗号 分隔.
	$tag = contentReplace($tag);
	$tag = implode(",",explode(" ",$tag));		//空格转换为逗号
	$tag = implode(",",explode("，",$tag));		//全角逗号转换为半角逗号
	$tag = implode(",",array_delnull(explode(",",$tag)));	//去空
	Return $tag;
}
function array_delnull($array){
	$array=array_filter($array,"notnull");
	Return $array;
}
function notnull($var){	//非空的保留
	Return !empty($var);
}
function rrand($len, $type = 7, $limit = false) {//1 - Number //2 - Lower Char //4 - Upper Char //8 - Chinese
	mt_srand((double)microtime() * 1000000);
	switch($type) {
		case 1:
			$charlist = '0123456789';
			break;
		case 2:
			$charlist = 'abcdefghijklmnopqrstuvwxyz';
			break;
		case 3:
			$charlist = '0123456789abcdefghijklmnopqrstuvwxyz';
			break;
		case 4:
			$charlist = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case 5:
			$charlist = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case 6:
			$charlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case 7:
			$charlist = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
	}
	$str = '';
	if($type == 8) {
		//$max = strlen($charlist) / 2;
		//for($i = 0; $i < $len; $i+=2) $str .= substr($charlist,mt_rand(0, $max) * 2,3);
		//$str = mb_convert_encoding($str,'utf-8','gbk');
	}else {
		$max = strlen($charlist) - 1;
		for($i = 0; $i < $len; $i++) $str .= $charlist[mt_rand(0, $max)];
		if($type == 1 && $limit !== false) while($str > $limit) $str = rrand($len, $type, $limit);
	}
	return $str;
}
//email函数 (邮件地址,标题,内容)
function sendmail($target, $title, $content){
	$headers =
		"From: work@chinapet.com\r\n" .
		"Reply-To: work@chinapet.com\r\n" .
		"Content-type: text/html; charset=gbk";
	$title=mb_convert_encoding($title, 'gbk', 'utf-8');
	$content=mb_convert_encoding($content, 'gbk', 'utf-8');
	Return mail($target, $title, $content, $headers);
}
//得到时间范围
function getTimeArea(){
	$h = date("G");
	if(0<=$h and $h<8){
		Return "早上好！";
	}elseif(8<=$h and $h<12){
		Return "上午好！";
	}elseif(12<=$h and $h<14){
		Return "中午好！";
	}elseif(14<=$h and $h<18){
		Return "下午好！";
	}elseif(18<=$h and $h<24){
		Return "晚上好！";
	}
}
//拼接图片地址
function getImgName($n,$s){
	if(empty($n)) Return;
	$ext = explode(".", $n);
	$nn = $ext[0].'_'.$s.'.'.$ext[1];
	Return $nn;
}
//字符串转换
function arrayToGB($utf8array){
	$GB_array=array();
	if(is_array($utf8array)){
		foreach($utf8array as $k => $v){
			if(is_array($v)) array_push($GB_array,arrayToGB($v));
			else $GB_array[$k]=mb_convert_encoding($v, "gb2312", "UTF-8");
		}
	return $GB_array;
	}else{
		return $utf8array;
	}
}